package com.example.lemis.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.model.dto.DoorplateApplyDTO;
import com.example.lemis.model.dto.DoorplateApprovalDTO;
import com.example.lemis.model.dto.DoorplateReissueDTO;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.DoorplateApplyVO;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import com.example.lemis.common.JsonResponse;
import com.example.lemis.service.IDoorplateApplyService;
import com.example.lemis.model.domain.DoorplateApply;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-24
 * @version v1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/doorplateApply")
public class DoorplateApplyController {

    private final Logger logger = LoggerFactory.getLogger( DoorplateApplyController.class );

    private final IDoorplateApplyService doorplateApplyService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<DoorplateApply> getById(@PathVariable("id") Long id)throws Exception {
        DoorplateApply doorplateApply = doorplateApplyService.getById(id);
        return JsonResponse.success(doorplateApply);
    }

    @GetMapping("")
    public JsonResponse<?> queryApplyList(@RequestParam("streetIds") List<Long> streetIds, PageDTO pageDTO)
            throws Exception {
        Page<DoorplateApplyVO> vos = doorplateApplyService.queryApplyList(streetIds, pageDTO);
        return JsonResponse.success(vos);
    }

    @PostMapping("")
    public JsonResponse<?> addDoorplateApply(@RequestBody DoorplateApplyDTO doorplateApply) throws Exception {
        DoorplateApplyVO savedDoorplateApply = doorplateApplyService.insertApply(doorplateApply);
        if (savedDoorplateApply == null) {
            return JsonResponse.failure("门牌申请失败");
        }
        return JsonResponse.success(savedDoorplateApply);
    }

    /**
     * 描述：补发门牌
     */
    @PostMapping("/reissue")
    public JsonResponse<?> doorplate_reissue(@RequestBody DoorplateReissueDTO doorplateReissue) throws Exception {
        DoorplateApply doorplateApply = doorplateApplyService.selectNearApply(doorplateReissue);
        if (doorplateApply != null && doorplateApply.getStatus() != doorplateApply.getLevels()) {
            return JsonResponse.failure("您已经申请过门牌");
        }
        DoorplateApplyVO savedDoorplateReissue = doorplateApplyService.reissueApply(doorplateReissue);
        if (savedDoorplateReissue == null) {
            return JsonResponse.failure("您还未申请门牌，请先申请门牌");
        }
        return JsonResponse.success(savedDoorplateReissue);
    }

    @PutMapping("/{id}/approval")
    public JsonResponse<?> approval(@PathVariable("id") Long id, @Validated @RequestBody DoorplateApprovalDTO dto)
            throws Exception {
        DoorplateApplyVO doorplateApplyVO = doorplateApplyService.approval(id, dto);
        if (doorplateApplyVO == null) {
            return JsonResponse.failure("审批失败");
        }
        return JsonResponse.success(doorplateApplyVO);
    }
}

